package com.wxb.controller;

import com.wxb.common.core.web.domain.AjaxResult;
import com.wxb.common.security.utils.SecurityUtils;
import com.wxb.domain.MallKeywordsEntity;
import com.wxb.service.IMallKeywordsService;
import com.wxb.domain.MallSearchHistoryEntity;
import com.wxb.service.IMallSearchHistoryService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.wxb.common.core.web.domain.AjaxResult.success;

/**
 * @Date 2025-05-28 10:03
 * @Version 1.0
 * @Description
 * @ClassName MallSearchController
 **/
@RestController
@RequestMapping("/search")
public class MallSearchController {
    @Autowired
    private IMallSearchHistoryService searchHistoryService;
    @Autowired
    private IMallKeywordsService keywordsService;

    @PostMapping("index")
    public AjaxResult index() {
        Map<String, Object> resultObj = new HashMap<>();
        Map<String, Object> param = new HashMap<>();
        param.put("isDefault", 1);
        param.put("page", 1);
        param.put("limit", 1);
        param.put("sidx", "id");
        param.put("order", "asc");
        List<MallKeywordsEntity> keywordsEntityList = keywordsService.queryList(param);
        //取出输入框默认的关键词
        MallKeywordsEntity defaultKeyword = null != keywordsEntityList && keywordsEntityList.size() > 0 ? keywordsEntityList.get(0) : null;
        //取出热闹关键词
        param = new HashMap<>();
        param.put("fields", "distinct keyword,is_hot");
        param.put("page", 1);
        param.put("limit", 10);
        param.put("sidx", "id");
        param.put("order", "asc");
        List<Map> hotKeywordList = keywordsService.hotKeywordList(param);
        //
        param = new HashMap<>();
        param.put("fields", "distinct keyword");
        param.put("page", 1);
        param.put("limit", 10);
        param.put("sidx", "id");
        param.put("order", "asc");
        List<MallSearchHistoryEntity> historyVoList = searchHistoryService.queryList(param);
        String[] historyKeywordList = new String[historyVoList.size()];
        if (null != historyVoList) {
            int i = 0;
            for (MallSearchHistoryEntity historyVo : historyVoList) {
                historyKeywordList[i] = historyVo.getKeyword();
                i++;
            }
        }
        //
        resultObj.put("defaultKeyword", defaultKeyword);
        resultObj.put("historyKeywordList", historyKeywordList);
        resultObj.put("hotKeywordList", hotKeywordList);
        return success(resultObj);
    }

    @ApiImplicitParams({@ApiImplicitParam(name = "keyword", value = "关键字", paramType = "path", required = true)})
    @PostMapping("helper")
    public AjaxResult helper(String keyword) {
        Map<String, Object> param = new HashMap<>();
        param.put("fields", "distinct keyword");
        param.put("keyword", keyword);
        param.put("limit", 10);
        param.put("offset", 0);
        List<MallKeywordsEntity> keywords = keywordsService.queryList(param);
        String[] keys = new String[keywords.size()];
        if (null != keywords) {
            int i = 0;
            for (MallKeywordsEntity keywordsVo : keywords) {
                keys[i] = keywordsVo.getKeyword();
                i++;
            }
        }
        //
        return success(keys);
    }

    @PostMapping("clearhistory")
    public Object clearhistory() {
        searchHistoryService.deleteByUserId(SecurityUtils.getUserId());
        return success();
    }
}
